"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ignore
Case Name   : 使用ignore hint向float4/float8类型列插入空数据
Create At   : 2023/02/23
Owner       : @kyeleze
Description :
    1、创建兼容B库
    2、进入B兼容数据库,创建包含float4/float8类型column的表格
    3、使用ignore hint进行insert操作,插入非法数据
    4、查询数据
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import Constant
from testcase.utils.Logger import Logger


class IgnoreTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.database_name = 't_ignore_database_0039'
        self.table_name1 = 't_ignore_table_0039_01'
        self.table_name2 = 't_ignore_table_0039_02'

    def test_ignore_insert(self):
        text = '--step1:创建兼容b库的数据库;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database_name};" \
                  f"create database {self.database_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      result, '执行失败' + text)

        text = '--step2:进入B兼容数据库,创建包含float4/float8类型column的表格 ' \
               'expect:成功'
        self.log.info(text)
        sql_cmd = f'drop database if exists {self.table_name1};' \
                  f'create table {self.table_name1}(c float4);' \
                  f'drop database if exists {self.table_name2};' \
                  f'create table {self.table_name2}(c float8);'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                               dbname=self.database_name)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.CREATE_TABLE_SUCCESS),
                         2, '执行失败' + text)

        text = '--step3:使用ignore hint进行insert操作,插入非法数据 expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert ignore  into {self.table_name1} values('');" \
                  f"insert ignore into {self.table_name2} values('');"
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                               dbname=self.database_name)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.INSERT_SUCCESS_MSG),
                         2, '执行失败' + text)

        text = '--step4:查询数据 expect:成功--'
        self.log.info(text)
        sql_cmd = f'select * from {self.table_name1};'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                               dbname=self.database_name)
        self.log.info(result)
        self.assertIn('0', result, '执行失败' + text)
        sql_cmd = f'select * from {self.table_name2};'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                               dbname=self.database_name)
        self.log.info(result)
        self.assertIn('0', result, '执行失败' + text)

    def tearDown(self):
        self.log.info('--清理环境--')
        sql_cmd = f'drop database {self.database_name};'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      result, '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')

